<template>
    <zh-app style="font-family: '阿里巴巴普惠体'">
         <!-- 导航栏 -->
        <zh-navbar title="人才子女就学申请" color="#85a5ff" linear="right" />
        <zh-spin :load="data.loading" tip="信息加载中，请耐心等候">
            <zh-sheet v-if="!data.regFlag && !data.loading">
                <zh-result
                status="lock"
                color="amber"
                :showBtn="true"
                :title="data.operation.title"
                :subTitle="data.operation.subTitle"
                btnText="返回"
                @click="goBack"
                ></zh-result>
            </zh-sheet>
            <zh-sheet v-if="data.regFlag" :padding="[0,0]">
              <zh-form
                ref="formRef"
                @submit="submit"
                v-model="data.form"
                inputAlign="right"
                :label-width="220"
                :margin="[0, 0]"
                :padding="[20, 0]"
              >
                <zh-form-select
                  field="areaCode"
                  @confirm="onConfirm"
                  v-if="data.areaCodeDict.length > 1"
                  :rules="[{ required: true, message: '请选择想要就读区县' }]"
                  placeholder="请选择想要就读区县"
                  v-model="data.form.areaCode"
                  :options="data.areaCodeDict"
                  label="就读区县"
                  required
                  :transprent="true"
                ></zh-form-select>
                <zh-form-input
                  field="fullName"
                  :rules="[{ required: true, message: '请填写儿童姓名' }]"
                  placeholder="请填写儿童姓名"
                  v-model="data.form.fullName"
                  label="儿童姓名"
                  required
                  showClear
                  :maxlength="100"
                  :transprent="true"
                ></zh-form-input>
                <zh-form-select
                  field="cardType"
                  :showClear="false"
                  :rules="[{ required: true, message: '请选择儿童证件类型' }]"
                  placeholder="请选择儿童证件类型"
                  v-model="data.form.cardType"
                  :options="data.dictData.cardType"
                  label="儿童证件类型"
                  required
                  :transprent="true"
                ></zh-form-select>
                <zh-form-input
                  field="idcard"
                  :type="data.form.cardType === '01' ? 'idcard' : 'text'"
                  :rules="[{ required: true, message: '请填写儿童证件号码' }]"
                  placeholder="请填写儿童证件号码"
                  v-model="data.form.idcard"
                  label="儿童证件号码"
                  required
                  :maxlength="20"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-datetime
                  v-if="data.form.cardType !== '01'"
                  field="birthYmd"
                  :rules="[{ required: true, message: '请填写儿童出生日期' }]"
                  placeholder="请填写儿童出生日期"
                  v-model="data.form.birthYmd"
                  label="出生日期"
                  required
                  showClear
                  model="day"
                  :transprent="true"
                ></zh-form-datetime>
                <zh-form-select
                  v-if="data.form.cardType !== '01'"
                  field="sex"
                  :rules="[{ required: true, message: '请选择儿童性别' }]"
                  placeholder="请选择儿童性别"
                  v-model="data.form.sex"
                  :options="data.sexDict"
                  label="性别"
                  required
                  showClear
                  :transprent="true"
                ></zh-form-select>
                <zh-form-select
                  field="nation"
                  :showClear="false"
                  :rules="[{ required: true, message: '请选择民族' }]"
                  placeholder="请选择儿童民族"
                  v-model="data.form.nation"
                  :options="data.dictData.nation"
                  label="民族"
                  required
                  :transprent="true"
                ></zh-form-select>
                <zh-form-input
                  field="talentFullName"
                  :rules="[{ required: true, message: '请填写人才姓名' }]"
                  placeholder="请填写人才姓名"
                  :maxlength="100"
                  required
                  v-model="data.form.talentFullName"
                  label="人才姓名"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-select
                  field="talentCardType"
                  :showClear="false"
                  required
                  :rules="[
                    { required: true, message: '请填写人才证件类型' },
                  ]"
                  placeholder="请填写人才证件类型"
                  v-model="data.form.talentCardType"
                  :options="data.dictData.cardType"
                  label="人才证件类型"
                  :transprent="true"
                ></zh-form-select>
                <zh-form-input
                  field="talentIdcard"
                  required
                  :type="data.form.talentCardType === '01' ? 'idcard' : 'text'"
                  :rules="[
                    { required: true, message: '请填写人才证件号码' },
                  ]"
                  placeholder="请填写人才证件号码"
                  v-model="data.form.talentIdcard"
                  label="人才证件号码"
                  :maxlength="20"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-select
                  field="relationshipType"
                  required
                  :showClear="false"
                  :rules="[
                    { required: true, message: '请选择与儿童关系' },
                  ]"
                  placeholder="请选择与儿童关系"
                  v-model="data.form.relationshipType"
                  :options="data.dictData.relationshipType"
                  label="与儿童关系"
                  :transprent="true"
                ></zh-form-select>
                <zh-form-select
                  field="educationType"
                  :showClear="false"
                  required
                  :rules="[
                    { required: true, message: '请选择学历/学位' },
                  ]"
                  placeholder="请选择学历/学位"
                  v-model="data.form.educationType"
                  :options="data.dictData.educationType"
                  label="学历/学位"
                  :transprent="true"
                ></zh-form-select>
                <zh-form-input
                  field="technicalTitle"
                  :rules="[{ required: true, message: '请填写职称/高级技师资格' }]"
                  placeholder="请填写职称/高级技师资格"
                  v-model="data.form.technicalTitle"
                  label="职称/高级技师资格"
                  required
                  :maxlength="100"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-input
                  field="telphone"
                  :rules="[{ required: true, message: '请填写联系电话/手机' }]"
                  number
                  placeholder="请填写联系电话/手机"
                  v-model="data.form.telphone"
                  label="联系电话"
                  required
                  :maxlength="20"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-input
                  field="workUnit"
                  :rules="[{ required: true, message: '请填写工作单位' }]"
                  placeholder="请填写工作单位"
                  v-model="data.form.workUnit"
                  label="工作单位"
                  :maxlength="100"
                  required
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-input
                  field="householdPlace"
                  :rules="[{ required: true, message: '请填写学生户籍地' }]"
                  placeholder="请填写学生户籍地"
                  v-model="data.form.householdPlace"
                  label="户籍地"
                  :maxlength="100"
                  required
                  showClear
                  :transprent="true"
                ></zh-form-input>
                
                <zh-form-input
                  field="address"
                  :rules="[{ required: true, message: '请填写学生现居住地' }]"
                  placeholder="请填写学生现居住地"
                  v-model="data.form.address"
                  label="现居住地"
                  :maxlength="100"
                  required
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-select
                  field="talentType"
                  :showClear="false"
                  :rules="[
                    { required: true, message: '请选择人才类型' },
                  ]"
                  placeholder="请选择人才类型"
                  required
                  v-model="data.form.talentType"
                  :options="data.dictData.talentType"
                  label="人才类型"
                  :transprent="true"
                ></zh-form-select>
                <zh-form-input
                  field="jobResume"
                  layout="vertical"
                  required
                  inputAlign="left"
                  :rules="[
                    { required: true, message: '请填写工作简历' },
                  ]"
                  v-model="data.form.jobResume"
                  :height="300"
                  label="工作简历"
                  type="textarea"
                  placeholder="请填写工作简历"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                <zh-form-select
                  field="cardType"
                  :showClear="false"
                  :rules="[{ required: true, message: '请选择儿童就读学段' }]"
                  placeholder="请选择儿童就读学段"
                  v-model="data.form.schoolCategory"
                  :options="data.dictData.schoolCategory"
                  label="就读学段"
                  required
                  :transprent="true"
                ></zh-form-select>
                <zh-form-select  required  label="意向学校"  suffix="tmicon-clock-fill" showClear :lazy="true" mode="single"
                  :fieldNames="{ label: 'shortName', value: 'orgId' }"
                  :rules="[
                    { required: true, message: '请选择意向学校' },
                  ]"
                  funcCode="eduschool0001"
                  placeholder="请选择意向学校"
                  type="list"
                  :transprent="true"
                  :queryParams="{permissionFlag:false,recruitStatus: '0',talentApplyFlag:'1',schoolCategory:data.form.schoolCategory}"
                  searchField="schoolName"
                  field="schoolIdIntention" v-model="data.form.schoolIdIntention"  >
                </zh-form-select>
                <zh-form-input
                  field="studyIntention"
                  layout="vertical"
                  required
                  inputAlign="left"
                  :rules="[
                    { required: true, message: '请填写就读意向说明' },
                  ]"
                  v-model="data.form.studyIntention"
                  :height="300"
                  :maxlength="500"
                  label="就读意向说明"
                  type="textarea"
                  placeholder="请填写就读意向说明"
                  showClear
                  :transprent="true"
                ></zh-form-input>
                
                <zh-form-upload
                  field="fileIdcard"
                  v-model:fileList="data.form.fileIdcardList"
                  v-model="data.form.fileIdcard"
                  label="身份证照片"
                  required
                  :fileType="['image']"
                  :rows="3"
                  :maxFile="2"
                  :width="620"
                  :rules="[
                    { required: true, message: '请上传人才本人身份证正反面' },
                  ]"
                >
                  <template v-slot:desc>
                    <zh-text
                      color="grey-darken-2"
                      :font-size="22"
                      label="人才本人身份证正反面"
                    ></zh-text>
                  </template>
                </zh-form-upload>
                <zh-form-upload
                  field="fileHousehold"
                  v-model:fileList="data.form.fileHouseholdList"
                  v-model="data.form.fileHousehold"
                  label="户口簿照片"
                  :maxFile="9"
                  required
                  :fileType="['image']"
                  :rows="3"
                  :width="620"
                  :rules="[
                    { required: true, message: '请上传户口本等照片，不少于2张' },
                  ]"
                >
                  <template v-slot:desc>
                    <zh-text
                      color="grey-darken-2"
                      :font-size="22"
                      label="户口簿户主页、本人页、申请入学子女页"
                    ></zh-text>
                  </template>
                </zh-form-upload>
                <zh-form-upload
                  field="fileTalent"
                  v-if="data.form.talentType==='1'||data.form.talentType==='2'"
                  v-model="data.form.fileTalent"
                  label="证明材料"
                  :maxFile="18"
                  required
                  :fileType="['image']"
                  :rows="3"
                  :width="620"
                >
                  <template v-slot:desc>
                    <zh-text
                      v-if="data.form.talentType==='1'"
                      color="grey-darken-2"
                      :font-size="22"
                      label="高层次人才证明材料：博士研究生毕业证书、教育部学信网电子备案表或正高级专业技术职务任职资格电子证书、评审文件、评审表或公布荣誉文件、荣誉证书。"
                    ></zh-text>
                    <zh-text
                      v-if="data.form.talentType==='2'"
                      color="grey-darken-2"
                      :font-size="22"
                      label="本人硕士研究生毕业证书、教育部学信网电子备案表或本人高级职称电子证书、评审文件、评审表"
                    ></zh-text>
                  </template>
                </zh-form-upload>
                <zh-form-upload
                  v-if="data.form.talentType==='1'||data.form.talentType==='2'"
                  field="fileContract"
                  v-model="data.form.fileContract"
                  label="用工合同"
                  :maxFile="9"
                  :fileType="['image']"
                  :rows="3"
                  :width="620"
                >
                  <template v-slot:desc>
                    <zh-text
                      color="grey-darken-2"
                      :font-size="22"
                      :label="'本人与东阳市用人单位签订的有效期内的用工合同，有东阳用人单位'+(data.year-1)+'年社保参保记录的可以不提供合同'"
                    ></zh-text>
                  </template>
                </zh-form-upload>
                <zh-form-upload
                  v-if="data.form.talentType==='2'"
                  field="fileRecommendation"
                  v-model="data.form.fileRecommendation"
                  label="企业推荐书"
                  :maxFile="1"
                  required
                  :fileType="['image']"
                  :rows="3"
                  :width="620"
                >
                  <template v-slot:desc>
                    <zh-text
                      color="grey-darken-2"
                      :font-size="22"
                      label="人才本人所在企业推荐书，需盖章。"
                    ></zh-text>
                  </template>
                </zh-form-upload>
                <zh-form-item :border="false">
                  <view class="flex flex-row">
                    <view class="flex-1 mr-32" style="margin: 0 auto">
                      <zh-button
                        form-type="submit"
                        :disabled="data.btnDisabled"
                        :loading="data.btnDisabled"
                        label="提交信息"
                        block
                        color="#85a5ff"
                        linear="right"
                      ></zh-button>
                    </view>
                  </view>
                </zh-form-item>
              </zh-form>
            </zh-sheet>
          
        </zh-spin>
        <zh-message ref="messageRef" :duration="2000"></zh-message>
        <zh-modal
          title="温馨提醒"
          content="登记成功，请等待工作人员审核,审核结果可在我的申请记录中查看"
          cancelText="前往申请记录"
          okText="再登记一个"
          :overlayClick="false"
          v-model:show="data.showSuccess"
          @ok="onOk"
          @cancel="onCancel"
        ></zh-modal>
    </zh-app>
</template>
<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app";
import * as util from "@/tool/function/util";
//--------------------------参数定义-------------------------------
const data = reactive({
    year:new Date().getFullYear(),
    loading:false,
    operation: { title: "系统异常", subTitle: "请退出重新打开页面" },
    //是否可以登记
    regFlag: false,
    areaCodeDict: [] as any,
    form:{cardType:'01',talentCardType:'01',nation:'01',fileIdcardList:[],fileHouseholdList:[]} as any,
    btnDisabled:false,
    showSuccess:false,
    sexDict: [
      {
        dictKey: "1",
        dictValue: "男",
      },
      {
        dictKey: "2",
        dictValue: "女",
      },
    ],
    dictData:{} as any
})
let formRef=ref();
let messageRef=ref();
// 初始化
onLoad((options: any) => {
  if (options.areaCode) {
    data.form.areaCode = options.areaCode;
  }
  getAreaCode();
  getDict();
});

//-------------------------调用方法-------------------
const getDict = () => {
		const params = {
			dictFields: 'cardType,relationshipType,talentType,educationType,nation,schoolCategory',
		};
		$api.common.getBatchDict.post(params).then((res: any) => {
			data.dictData = res.result;
		});
};
//获取可选择的行政区划
const getAreaCode = () => {
  data.loading = true;
  $api.common.send
    .post("dict0028", {})
    .then((res: any) => {
      data.areaCodeDict = res.result || [];
      if (data.areaCodeDict.length === 1) {
        data.form.areaCode = data.areaCodeDict[0].dictKey;
        getOp();
      } else if (data.areaCodeDict.length > 1) {
        let flag = false;
        if(data.form.areaCode){
            data.areaCodeDict.forEach((item :any) => {
            if (item.dictKey == data.form.areaCode) {
                flag = true;
                getOp();
            }
            });
        }
       
        if (!flag) {
            data.loading = false;
          data.form.areaCode = "";
        }
      }
    })
    .catch(() => {data.loading = false;});
};
const onConfirm = () => {
  //行政区划变动选择
  getOp();
};
//获取业务期
const getOp = () => {
  let params = {
    operating: "YW054",
    classify: "2",
    areaCode: data.form.areaCode,
  };
  data.loading = true;
  $api.common.send
    .post("OperationCache0003", params)
    .then((res: any) => {
      let result = res.result;
      data.operation.title = "不能办理业务";
      if (result && result.flag) {
        data.regFlag = true;
      } else {
        if (result) {
          if (result.type === "1" && result.messagePrompts) {
            data.operation.subTitle = result.messagePrompts;
          } else if (result.type === "2" && result.messageTip) {
            data.operation.subTitle = result.messageTip;
          } else {
            data.operation.subTitle =
              result.year + "年度义务教育阶段人才子就学申请工作尚未开始";
          }
        }
        data.regFlag = false;
      }
      data.loading = false;
    })
    .catch(() => {
      data.regFlag = false;
      data.loading = false;
    });
};
// 提交
const submit = (e: any) => {
  if (e.validate) {
    if (data.form.fileIdcardList.length < 2) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "请上传身份证正反面照片信息！";
      messageRef.value.show(messageData);
      return;
    }
    if (data.form.fileHouseholdList.length < 2) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "请上传不少于2张户口本信息！";
      messageRef.value.show(messageData);
      return;
    
    }
    if (data.form.cardType === "01" && !util.isIdCard(data.form.idcard)) {
      let messageData: any = {};
      messageData.model = "warn";
      messageData.text = "请填写正确的学生身份证！";
      messageRef.value.show(messageData);
      return;
    }
    if (data.form.talentIdcard) {
      if (
        data.form.talentCardType === "01" &&
        !util.isIdCard(data.form.talentIdcard)
      ) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请填写正确的人才身份证！";
        messageRef.value.show(messageData);
        return;
      }
    }
    if (data.form.telphone&&data.form.telphone.length>=11) {
      if (!util.isPhone(data.form.telphone)) {
        let messageData: any = {};
        messageData.model = "warn";
        messageData.text = "请填写正确的手机号码！";
        messageRef.value.show(messageData);
        return;
      }
    }
    //校验通过
    data.btnDisabled = true;
    $api.common.send
      .post("eduTalent0009", data.form)
      .then((res: any) => {
        //保存成功后提示
        data.showSuccess = true;
        data.form.fullName=''
        data.form.idcard=''
        data.form.cardType='01'
        data.form.relationshipType=''
        data.btnDisabled = false;
      })
      .catch(() => {
        data.btnDisabled = false;
      });
  }
};
const goBack=()=>{
  $router.back(1)
}
const onCancel = () => {
  $router.replace("/pages/edu/myApply/myApply", {});
};
const onOk = () => {
  data.showSuccess = false;
};
</script>